﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    ViewData["Title"] = "新增维修单";
    Layout = null;
}
@Html.AntiForgeryToken()
@using Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增维修单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="~/libs/scripts/layui-v2.5.6/layui/css/layui.css" media="all">
    <style>
        body {
            background: #f8f9fa;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
        }

        .page-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px 0;
            margin-bottom: 20px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }

        .page-title {
            font-size: 28px;
            font-weight: 600;
            margin: 0;
            text-align: center;
        }

        .page-subtitle {
            text-align: center;
            margin-top: 8px;
            opacity: 0.9;
            font-size: 14px;
        }

        .form-container {
            background: white;
            border-radius: 8px;
            padding: 30px;
            box-shadow: 0 2px 12px rgba(0,0,0,0.08);
            margin-bottom: 20px;
        }

        .section-title {
            font-size: 18px;
            font-weight: 600;
            color: #333;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #f0f0f0;
        }

        .layui-form-label {
            width: 120px;
            font-weight: 500;
            text-align: right;
            padding-right: 15px;
        }
        
        .layui-form-item {
            margin-bottom: 25px;
        }
        
        .layui-input-block {
            margin-left: 135px;
        }

        /* 卡片组样式 */
        .form-group-card {
            background: #fafbfc;
            border: 1px solid #e8eaed;
            border-radius: 8px;
            padding: 25px;
            margin-bottom: 25px;
        }

        .group-title {
            font-size: 16px;
            font-weight: 600;
            color: #333;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .group-title i {
            color: #667eea;
            font-size: 18px;
        }

        /* 两列布局 */
        .form-row {
            display: flex;
            gap: 30px;
            margin-bottom: 25px;
        }

        .form-col {
            flex: 1;
        }

        .form-col .layui-form-item {
            margin-bottom: 0;
        }

        .form-col .layui-form-label {
            width: 100px;
        }

        .form-col .layui-input-block {
            margin-left: 115px;
        }

        /* 全宽字段 */
        .form-full-width {
            width: 100%;
        }

        .form-full-width .layui-form-label {
            width: 120px;
        }

        .form-full-width .layui-input-block {
            margin-left: 135px;
        }

        .layui-input, .layui-textarea {
            border-radius: 4px;
            border: 1px solid #e6e6e6;
            transition: all 0.3s;
        }

        .layui-input:focus, .layui-textarea:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
        }

        /* 确保下拉框使用layui原生样式 */
        .layui-form-select .layui-input {
            padding-right: 30px !important;
        }

        .layui-btn {
            border-radius: 6px;
            font-weight: 500;
            transition: all 0.3s;
            padding: 0 25px;
            height: 40px;
            line-height: 40px;
        }

        .layui-btn:hover {
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(0,0,0,0.15);
        }

        .btn-group {
            text-align: center;
            padding: 30px 0;
            border-top: 1px solid #f0f0f0;
            margin-top: 30px;
        }

        .btn-group .layui-btn {
            margin: 0 10px;
            min-width: 100px;
        }

        .required {
            color: #ff5722;
        }

        .form-tips {
            color: #999;
            font-size: 12px;
            margin-top: 5px;
        }

        .auto-generate {
            display: inline-flex;
            align-items: center;
            gap: 10px;
        }

        .auto-generate .layui-input {
            flex: 1;
        }

        .auto-generate .layui-btn {
            height: 38px;
            line-height: 38px;
            padding: 0 15px;
            font-size: 12px;
        }

        /* 卡片悬停效果 */
        .form-group-card:hover {
            box-shadow: 0 4px 20px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
        }

        /* 响应式设计 */
        @@media (max-width: 768px) {
            .form-container {
                padding: 15px;
            }
            
            .form-group-card {
                padding: 20px 15px;
            }
            
            .form-row {
                flex-direction: column;
                gap: 0;
            }
            
            .form-col .layui-form-label {
                width: 90px;
            }
            
            .form-col .layui-input-block {
                margin-left: 105px;
            }
            
            .form-full-width .layui-form-label {
                width: 90px;
            }
            
            .form-full-width .layui-input-block {
                margin-left: 105px;
            }

            .group-title {
                font-size: 15px;
            }

            .auto-generate {
                flex-direction: column;
                gap: 10px;
            }

            .auto-generate .layui-btn {
                width: 100%;
            }
        }

        @@media (max-width: 480px) {
            .page-header {
                padding: 20px 0;
            }

            .page-title {
                font-size: 22px;
            }

            .form-container {
                padding: 10px;
            }

            .form-group-card {
                padding: 15px 10px;
            }
        }
    </style>
</head>
<body>
    <!-- 页面头部 -->
    <div class="page-header">
        <div class="layui-container">
            <h1 class="page-title">新增维修单</h1>
            <p class="page-subtitle">Create Repair Order</p>
        </div>
    </div>

    <div class="layui-container">
        <div class="form-container">
            <form class="layui-form" lay-filter="repairOrderForm" id="repairOrderForm">
                
                <!-- 基本信息组 -->
                <div class="form-group-card">
                    <div class="group-title">
                        <i class="layui-icon layui-icon-form"></i>
                        基本信息
                    </div>
                    
                    <!-- 维修单编号 -->
                    <div class="layui-form-item form-full-width">
                        <label class="layui-form-label"><span class="required">*</span>维修单编号</label>
                        <div class="layui-input-block">
                            <div class="auto-generate">
                                <input type="text" name="repairOrderCode" required lay-verify="required|repairOrderCode" placeholder="请输入维修单编号" autocomplete="off" class="layui-input" readonly>
                                <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="generateCodeBtn">
                                    <i class="layui-icon layui-icon-refresh"></i> 自动生成
                                </button>
                            </div>
                            <div class="form-tips">维修单编号唯一，建议使用自动生成功能</div>
                        </div>
                    </div>

                    <!-- 维修单名称 -->
                    <div class="layui-form-item form-full-width">
                        <label class="layui-form-label"><span class="required">*</span>维修单名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="repairOrderName" required lay-verify="required|repairOrderName" placeholder="请输入维修单名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>

                    <!-- 单据状态 -->
                    <div class="form-row">
                        <div class="form-col">
                            <div class="layui-form-item">
                                <label class="layui-form-label"><span class="required">*</span>单据状态</label>
                                <div class="layui-input-block">
                                    <select name="documentStatus" lay-verify="required|documentStatus">
                                        <option value="0" selected>待处理</option>
                                        <option value="1">已完成</option>
                                        <option value="2">已取消</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="form-col">
                            <!-- 占位列 -->
                        </div>
                    </div>
                </div>

                <!-- 设备信息组 -->
                <div class="form-group-card">
                    <div class="group-title">
                        <i class="layui-icon layui-icon-set"></i>
                        设备信息
                    </div>
                    
                    <div class="form-row">
                        <div class="form-col">
                            <div class="layui-form-item">
                                <label class="layui-form-label"><span class="required">*</span>设备编码</label>
                                <div class="layui-input-block">
                                    <input type="text" name="equipmentCode" required lay-verify="required|equipmentCode" placeholder="请输入设备编码" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="form-col">
                            <div class="layui-form-item">
                                <label class="layui-form-label"><span class="required">*</span>设备名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="equipmentName" required lay-verify="required|equipmentName" placeholder="请输入设备名称" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 日期信息组 -->
                <div class="form-group-card">
                    <div class="group-title">
                        <i class="layui-icon layui-icon-date"></i>
                        日期信息
                    </div>
                    
                    <div class="form-row">
                        <div class="form-col">
                            <div class="layui-form-item">
                                <label class="layui-form-label"><span class="required">*</span>报修日期</label>
                                <div class="layui-input-block">
                                    <input type="text" name="warrantyDate" id="warrantyDate" required lay-verify="required|warrantyDate" placeholder="请选择报修日期" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="form-col">
                            <div class="layui-form-item">
                                <label class="layui-form-label">维修完成日期</label>
                                <div class="layui-input-block">
                                    <input type="text" name="completionDate" id="completionDate" lay-verify="completionDate" placeholder="请选择维修完成日期" autocomplete="off" class="layui-input">
                                    <div class="form-tips">可选，维修完成后再填写</div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-col">
                            <div class="layui-form-item">
                                <label class="layui-form-label">验收日期</label>
                                <div class="layui-input-block">
                                    <input type="text" name="acceptanceDate" id="acceptanceDate" lay-verify="acceptanceDate" placeholder="请选择验收日期" autocomplete="off" class="layui-input">
                                    <div class="form-tips">可选，验收时填写</div>
                                </div>
                            </div>
                        </div>
                        <div class="form-col">
                            <!-- 占位列 -->
                        </div>
                    </div>
                </div>

                <!-- 维修信息组 -->
                <div class="form-group-card">
                    <div class="group-title">
                        <i class="layui-icon layui-icon-username"></i>
                        维修信息
                    </div>
                    
                    <div class="form-row">
                        <div class="form-col">
                            <div class="layui-form-item">
                                <label class="layui-form-label">维修结果</label>
                                <div class="layui-input-block">
                                    <select name="repairResults" lay-verify="">
                                        <option value="">请选择维修结果</option>
                                        <option value="维修完成">维修完成</option>
                                        <option value="部分完成">部分完成</option>
                                        <option value="无法维修">无法维修</option>
                                        <option value="待配件">待配件</option>
                                    </select>
                                    <div class="form-tips">可选，维修进行时或完成后填写</div>
                                </div>
                            </div>
                        </div>
                        <div class="form-col">
                            <div class="layui-form-item">
                                <label class="layui-form-label">维修人员</label>
                                <div class="layui-input-block">
                                    <input type="text" name="repairPersonnel" lay-verify="repairPersonnel" placeholder="请输入维修人员姓名" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-col">
                            <div class="layui-form-item">
                                <label class="layui-form-label">验收人员</label>
                                <div class="layui-input-block">
                                    <input type="text" name="acceptancePersonnel" lay-verify="acceptancePersonnel" placeholder="请输入验收人员姓名" autocomplete="off" class="layui-input">
                                    <div class="form-tips">可选，验收时填写</div>
                                </div>
                            </div>
                        </div>
                        <div class="form-col">
                            <!-- 占位列 -->
                        </div>
                    </div>
                </div>

                <!-- 按钮组 -->
                <div class="btn-group">
                    <button class="layui-btn layui-btn-lg" lay-submit lay-filter="submitForm">
                        <i class="layui-icon layui-icon-ok"></i> 保存
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary layui-btn-lg">
                        <i class="layui-icon layui-icon-refresh"></i> 重置
                    </button>
                    <button type="button" class="layui-btn layui-btn-normal layui-btn-lg" id="backBtn">
                        <i class="layui-icon layui-icon-return"></i> 返回
                    </button>
                </div>
            </form>
        </div>
    </div>

    <script src="~/libs/scripts/layui-v2.5.6/layui/layui.js"></script>
    <script>
    layui.use(['form', 'layer', 'laydate'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var $ = layui.$;

        // 获取token
        var token = $('input[name="__RequestVerificationToken"]').val();

        // 初始化日期选择器
        laydate.render({
            elem: '#warrantyDate',
            type: 'date',
            value: new Date(),
            max: 0 // 不能选择今天之后的日期
        });

        laydate.render({
            elem: '#completionDate',
            type: 'date'
        });

        laydate.render({
            elem: '#acceptanceDate',
            type: 'date'
        });

        // 页面加载时自动生成维修单编号
        $(document).ready(function(){
            generateRepairOrderCode();
        });

        // 生成维修单编号
        $('#generateCodeBtn').on('click', function(){
            generateRepairOrderCode();
        });

        function generateRepairOrderCode() {
            $.ajax({
                url: '/DeviceLedger/GenerateRepairOrderCode',
                type: 'GET',
                success: function(result){
                    if(result.code === 0){
                        $('input[name="repairOrderCode"]').val(result.repairOrderCode);
                    } else {
                        layer.msg(result.msg || '生成编号失败', {icon: 2});
                    }
                },
                error: function(){
                    layer.msg('生成编号失败', {icon: 2});
                }
            });
        }

        // 自定义验证规则
        form.verify({
            repairOrderCode: function(value){
                if(!value || value.trim() === ''){
                    return '维修单编号不能为空';
                }
                if(value.length > 50){
                    return '维修单编号长度不能超过50个字符';
                }
            },
            repairOrderName: function(value){
                if(!value || value.trim() === ''){
                    return '维修单名称不能为空';
                }
                if(value.length > 100){
                    return '维修单名称长度不能超过100个字符';
                }
            },
            equipmentCode: function(value){
                if(!value || value.trim() === ''){
                    return '设备编码不能为空';
                }
                if(value.length > 50){
                    return '设备编码长度不能超过50个字符';
                }
            },
            equipmentName: function(value){
                if(!value || value.trim() === ''){
                    return '设备名称不能为空';
                }
                if(value.length > 100){
                    return '设备名称长度不能超过100个字符';
                }
            },
            warrantyDate: function(value){
                if(!value || value.trim() === ''){
                    return '报修日期不能为空';
                }
                // 验证日期格式
                var dateReg = /^\d{4}-\d{2}-\d{2}$/;
                if(!dateReg.test(value)){
                    return '报修日期格式不正确，请选择日期';
                }
            },
            completionDate: function(value){
                if(value && value.trim() !== ''){
                    var dateReg = /^\d{4}-\d{2}-\d{2}$/;
                    if(!dateReg.test(value)){
                        return '维修完成日期格式不正确，请选择日期';
                    }
                    // 验证完成日期不能早于报修日期
                    var warrantyDate = $('input[name="warrantyDate"]').val();
                    if(warrantyDate && value < warrantyDate){
                        return '维修完成日期不能早于报修日期';
                    }
                }
            },
            acceptanceDate: function(value){
                if(value && value.trim() !== ''){
                    var dateReg = /^\d{4}-\d{2}-\d{2}$/;
                    if(!dateReg.test(value)){
                        return '验收日期格式不正确，请选择日期';
                    }
                    // 验证验收日期不能早于报修日期
                    var warrantyDate = $('input[name="warrantyDate"]').val();
                    if(warrantyDate && value < warrantyDate){
                        return '验收日期不能早于报修日期';
                    }
                }
            },
            documentStatus: function(value){
                if(value === '' || value === null || value === undefined){
                    return '请选择单据状态';
                }
            },
            repairPersonnel: function(value){
                if(value && value.length > 50){
                    return '维修人员姓名长度不能超过50个字符';
                }
            },
            acceptancePersonnel: function(value){
                if(value && value.length > 50){
                    return '验收人员姓名长度不能超过50个字符';
                }
            }
        });

        // 表单提交
        form.on('submit(submitForm)', function(data){
            var loading = layer.load(2, {content: '正在保存...'});
            
            // 处理表单数据
            var formData = $.extend({}, data.field);
            
            // 验证必填字段
            if (!formData.repairOrderCode || formData.repairOrderCode.trim() === '') {
                layer.close(loading);
                layer.msg('维修单编号不能为空', {icon: 2});
                return false;
            }
            if (!formData.repairOrderName || formData.repairOrderName.trim() === '') {
                layer.close(loading);
                layer.msg('维修单名称不能为空', {icon: 2});
                return false;
            }
            if (!formData.equipmentCode || formData.equipmentCode.trim() === '') {
                layer.close(loading);
                layer.msg('设备编码不能为空', {icon: 2});
                return false;
            }
            if (!formData.equipmentName || formData.equipmentName.trim() === '') {
                layer.close(loading);
                layer.msg('设备名称不能为空', {icon: 2});
                return false;
            }
            if (!formData.warrantyDate || formData.warrantyDate.trim() === '') {
                layer.close(loading);
                layer.msg('报修日期不能为空', {icon: 2});
                return false;
            }
            if (!formData.documentStatus) {
                layer.close(loading);
                layer.msg('请选择单据状态', {icon: 2});
                return false;
            }
            
            // 确保必填日期字段有正确格式
            if (!formData.warrantyDate || formData.warrantyDate.trim() === '') {
                formData.warrantyDate = new Date().toISOString().split('T')[0]; // 默认今天
            }
            
            // 处理可选日期字段，为空时设置为后端能接受的默认值
            if (!formData.completionDate || formData.completionDate.trim() === '') {
                formData.completionDate = '1900-01-01T00:00:00';
            } else {
                formData.completionDate = formData.completionDate + 'T00:00:00';
            }
            if (!formData.acceptanceDate || formData.acceptanceDate.trim() === '') {
                formData.acceptanceDate = '1900-01-01T00:00:00';
            } else {
                formData.acceptanceDate = formData.acceptanceDate + 'T00:00:00';
            }
            
            // 确保报修日期有正确的时间格式
            if (formData.warrantyDate && !formData.warrantyDate.includes('T')) {
                formData.warrantyDate = formData.warrantyDate + 'T00:00:00';
            }
            
            // 处理可选字符串字段
            if (!formData.repairResults || formData.repairResults.trim() === '') {
                formData.repairResults = '';
            }
            if (!formData.repairPersonnel || formData.repairPersonnel.trim() === '') {
                formData.repairPersonnel = '';
            }
            if (!formData.acceptancePersonnel || formData.acceptancePersonnel.trim() === '') {
                formData.acceptancePersonnel = '';
            }
            
            $.ajax({
                url: '/DeviceLedger/AddRepairOrder',
                type: 'POST',
                data: formData,
                headers: {
                    'RequestVerificationToken': token
                },
                success: function(result){
                    layer.close(loading);
                    if(result.code === 0){
                        layer.msg('保存成功', {icon: 1}, function(){
                            // 跳转回列表页面
                            window.location.href = '/DeviceLedger/RepairOrderView';
                        });
                    } else {
                        layer.msg(result.msg || '保存失败', {icon: 2});
                    }
                },
                error: function(){
                    layer.close(loading);
                    layer.msg('保存失败，请重试', {icon: 2});
                }
            });
            return false;
        });

        // 返回按钮
        $('#backBtn').on('click', function(){
            layer.confirm('确定要返回吗？未保存的数据将会丢失。', {
                icon: 3,
                title: '确认返回'
            }, function(index){
                window.location.href = '/DeviceLedger/RepairOrderView';
                layer.close(index);
            });
        });

        // 重置表单后重新生成编号
        $('button[type="reset"]').on('click', function(){
            setTimeout(function(){
                generateRepairOrderCode();
            }, 100);
        });
    });
    </script>
</body>
</html>
